package com.ncmem.down3.biz;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.google.gson.Gson;
import com.ncmem.down3.model.DnFileInf;
import com.ncmem.up7.DbHelper;

public class DnFile
{
    public DnFile()
    {
    }

    public void Add(DnFileInf inf)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("insert into down3_files(");
        sb.append(" f_id");
        sb.append(",f_uid");
        sb.append(",f_mac");
        sb.append(",f_nameLoc");
        sb.append(",f_pathLoc");
        sb.append(",f_fileUrl");
        sb.append(",f_lenSvr");
        sb.append(",f_sizeLoc");
        sb.append(",f_sizeSvr");
        sb.append(",f_fdTask");

        sb.append(") values(");

        sb.append(" ?");//id
        sb.append(",?");//uid
        sb.append(",?");//mac
        sb.append(",?");//nameLoc
        sb.append(",?");//pathLoc
        sb.append(",?");//fileUrl
        sb.append(",?");//lenSvr
        sb.append(",?");//sizeLoc
        sb.append(",?");//sizeSvr
        sb.append(",?");//fdTask
        sb.append(")");

        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sb.toString());

        try
        {
            cmd.setString (1 ,inf.id);
            cmd.setInt    (2 ,inf.uid);
            cmd.setString (3 ,inf.mac);
            cmd.setString (4 ,inf.nameLoc);
            cmd.setString (5 ,inf.pathLoc);
            cmd.setString (6 ,inf.fileUrl);
            cmd.setLong   (7 ,inf.lenSvr);
            cmd.setString (8 ,inf.sizeLoc);
            cmd.setString (9 ,inf.sizeSvr);
            cmd.setBoolean(10,inf.fdTask);
            db.ExecuteNonQuery(cmd);
        }
        catch (SQLException e){e.printStackTrace();}
    }

    public void Delete(String fid,Integer uid)
    {
        String sql = "delete from down3_files where f_id=? and f_uid=?";
        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sql);

        try
        {
            cmd.setString(1,fid);
            cmd.setInt(2,uid );
            db.ExecuteNonQuery(cmd);
        }
        catch (SQLException e){e.printStackTrace();}
    }

    /**
     * 更新文件进度信息
     * @param fid
     * @param uid
     * @param sizeLoc
     * @param perLoc
     */
    public void process(String fid,int uid,String sizeLoc,String perLoc)
    {
        String sql = "update down3_files set f_sizeLoc=?,f_perLoc=? where f_id=? and f_uid=?";
        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sql);

        try
        {
            cmd.setString(1,sizeLoc);
            cmd.setString(2,perLoc);
            cmd.setString(3,fid);
            cmd.setInt(4,uid);

            db.ExecuteNonQuery(cmd);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public void Clear()
    {
        DbHelper db = new DbHelper();
        db.ExecuteNonQuery("truncate table down3_files");
        //db.ExecuteNonQuery("truncate table hup_folders");
    }

    public String all_uncmp(int uid)
    {
        List<DnFileInf> files = new ArrayList<DnFileInf>();
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append(" f_id");//1
        sb.append(",f_nameLoc");//2
        sb.append(",f_pathLoc");//3
        sb.append(",f_perLoc");//4
        sb.append(",f_sizeSvr");//5
        sb.append(",f_fdTask");//6
        //
        sb.append(" from down3_files");
        //
        sb.append(" where f_uid=? ");

        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sb.toString());

        try
        {
            cmd.setInt(1, uid);
            ResultSet r = db.ExecuteDataSet(cmd);
            while(r.next())
            {
                DnFileInf f = new DnFileInf();
                f.id        = r.getString(1);
                f.nameLoc   = r.getString(2);
                f.pathLoc   = r.getString(3);
                f.perLoc    = r.getString(4);
                f.sizeSvr   = r.getString(5);
                f.fdTask    = r.getBoolean(6);
                files.add(f);
            }
            cmd.getConnection().close();
            cmd.close();//auto close ResultSet
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (files.size() > 0)
        {
            Gson gson = new Gson();
            return gson.toJson(files);
        }
        return "";
    }

    public String all_complete(int uid)
    {
        List<DnFileInf> files = new ArrayList<DnFileInf>();
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append(" up7_files.f_id");//1
        sb.append(",up7_files.f_fdTask");//2
        sb.append(",up7_files.f_nameLoc");//3
        sb.append(",up7_files.f_sizeLoc");//4
        sb.append(",up7_files.f_lenSvr");//5
        sb.append(",up7_files.f_pathSvr");//6
        sb.append(",up7_files.f_blockPath");//7
        sb.append(",f_fileCount");//8
        sb.append(" from up7_files ");
        sb.append(" left join up7_folders on up7_folders.f_id=up7_files.f_id");
        //
        sb.append(" where up7_files.f_uid=? and up7_files.f_deleted=0 and up7_files.f_complete=1 and up7_files.f_merged=1 and up7_files.f_fdChild=0");

        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sb.toString());

        try
        {
            cmd.setInt(1, uid);
            ResultSet r = db.ExecuteDataSet(cmd);
            while(r.next())
            {
                DnFileInf f = new DnFileInf();
                String uuid = UUID.randomUUID().toString();
                uuid = uuid.replace("-", "");

                f.id        = uuid;
                f.f_id    = r.getString(1);
                f.fdTask    = r.getBoolean(2);
                f.nameLoc   = r.getString(3);
                f.sizeLoc   = "0byte";
                f.sizeSvr   = r.getString(4);
                f.lenSvr    = r.getLong(5);
                f.pathSvr   = r.getString(6);
                f.blockPath = r.getString(7);
                f.fileCount = r.getInt(8);
                files.add(f);
            }
            cmd.getConnection().close();
            cmd.close();//auto close ResultSet
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (files.size() > 0)
        {
            Gson gson = new Gson();
            return gson.toJson(files);
        }
        return "";
    }
}